package it.storm.solution;

import java.util.ArrayList;
import java.util.List;

/**
 * 766. 托普利茨矩阵
 * https://leetcode-cn.com/problems/toeplitz-matrix/
 */
public class Solutions_766 {
    public static void main(String[] args) {
        int[][] matrix1 = {{1, 2, 3, 4},
                {5, 1, 2, 3},
                {9, 5, 1, 2}};  // output: true

        int[][] matrix2 = {{1, 2}, {2, 2}};  // output: false

        List<int[][]> inputs = new ArrayList<>();
        inputs.add(matrix1);
        inputs.add(matrix2);

        for (int[][] input : inputs) {
            boolean result = isToeplitzMatrix(input);
            System.out.println(result);
        }
    }

    public static boolean isToeplitzMatrix(int[][] matrix) {
        if (matrix == null) {
            return true;
        }
        int row = matrix.length;
        if (row <= 1) {
            return true;
        }
        int col = matrix[0].length;
        // 除第 1 行、第 1 列，其余元素都与其左上角元素比较是否相同
        for (int i = 1; i < row; i++) {
            for (int j = 1; j < col; j++) {
                if (matrix[i][j] != matrix[i - 1][j - 1]) {
                    return false;
                }
            }
        }
        return true;
    }
}
